package com.shengdong.water.code.viewmodel

import com.shengdong.water.base.BaseViewModel
import com.shengdong.water.code.bean.RealTimeBean
import com.shengdong.water.code.repository.ApiRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import javax.inject.Inject

@HiltViewModel
class MonitorDetailViewModel @Inject constructor(
    private val repository: ApiRepository
) : BaseViewModel()  {

    private val _detail = MutableStateFlow<RealTimeBean?>(null)
    val detail: StateFlow<RealTimeBean?> = _detail


    fun getDetail(id: String) {
        handleRequest(
            loading = "加载中...",
            flow = repository.getRealTimeDetail(id),
            onSuccess = { _detail.value = it },
            onError = { code ,msg ->
                when (code) {
                    401 ->  {}// 处理未授权
                    403 -> {}// 处理禁止访问
                    else -> showError(msg)
                }
            },
            onToast =  {
                it
            },
            onException = {e ->
                showError("发生错误: ${e.message}")
            },
        )
    }
}